package cn.xo68.boot.auth.server.service;

import cn.xo68.boot.auth.core.common.AuthConstants;
import cn.xo68.boot.auth.server.domain.OauthResource;
import cn.xo68.boot.auth.server.mapper.OauthResourceMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * OauthResource Service
 * @author wuyy(16349023@qq.com)
 * @date 2018年08月28日 11:04:12
 *
 */
@CacheConfig(cacheNames = {AuthConstants.CACHE_NAME_OAUTH2_RESOURCE})
 @Service
public class OauthResourceService{
    @Autowired
    private OauthResourceMapper oauthResourceMapper;

    public OauthResource get(String clientId,String resourceId) {
        return  oauthResourceMapper.get(clientId,resourceId);
    }
    public OauthResource getMaxChild(String clientId,String parentId) {
        return  oauthResourceMapper.getMaxChild(clientId,parentId);
    }

    @CacheEvict(allEntries = true)
    public void insert(OauthResource oauthResource) {
        oauthResourceMapper.insert(oauthResource);
    }

    @CacheEvict(allEntries = true)
    public void update(OauthResource oauthResource) {
        oauthResourceMapper.update(oauthResource);
    }

    @CacheEvict(allEntries = true)
    public void delete(String clientId,String resourceId) {
        oauthResourceMapper.delete(clientId,resourceId);
    }

    @Cacheable(key = "#clientId",unless = "#result == null ")
    public List<OauthResource> list(String clientId) {
        return oauthResourceMapper.list(clientId);
    }

    public List<OauthResource> listchild(String clientId,String parentId){
        return oauthResourceMapper.listChild(clientId,parentId);
    }
	
	public PageInfo<OauthResource> list(String clientId,int pageNumber,int pageSize) {
        PageHelper.startPage(pageNumber, pageSize,true);
        List<OauthResource> docs = oauthResourceMapper.list(clientId);
        PageInfo<OauthResource> pageInfo = new PageInfo<>(docs);
        return pageInfo;
    }
}